package com.atguigu.daijia.driver.service.impl;

import com.atguigu.daijia.common.constant.RedisConstant;
import com.atguigu.daijia.common.execption.GuiguException;
import com.atguigu.daijia.common.result.ResultCodeEnum;
import com.atguigu.daijia.dispatch.client.NewOrderFeignClient;
import com.atguigu.daijia.driver.client.DriverInfoFeignClient;
import com.atguigu.daijia.driver.service.DriverService;
import com.atguigu.daijia.map.client.LocationFeignClient;
import com.atguigu.daijia.model.vo.driver.DriverLoginVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Slf4j
@Service
@SuppressWarnings({"unchecked", "rawtypes"})
@RequiredArgsConstructor
public class DriverServiceImpl implements DriverService {

    private final DriverInfoFeignClient driverInfoFeignClient;

    private final RedisTemplate redisTemplate;

    private final LocationFeignClient locationFeignClient;

    private final NewOrderFeignClient newOrderFeignClient;

    /**
     * 司机端小程序授权登录
     * @param code
     * @return
     */
    @Override
    public String login(String code) {
        Long driverId = driverInfoFeignClient.login(code).getData();
        String token = UUID.randomUUID().toString().replace("-","");
        redisTemplate.opsForValue().set(RedisConstant.USER_LOGIN_KEY_PREFIX+token,
                                        driverId.toString(),
                                        RedisConstant.USER_LOGIN_KEY_TIMEOUT,
                                        TimeUnit.SECONDS);
        return token;
    }

    /**
     * 获取司机登录信息
     * @param driverId
     * @return
     */
    @Override
    public DriverLoginVo getDriverLoginInfo(Long driverId) {
        return driverInfoFeignClient.getDriverLoginInfo(driverId).getData();
    }

    /**
     * 判断司机当日是否进行过人脸识别
     * @param driverId
     * @return
     */
    @Override
    public Boolean isFaceRecognition(Long driverId) {
        return driverInfoFeignClient.isFaceRecognition(driverId).getData();
    }

    /**
     * 开始接单服务
     * @param driverId
     * @return
     */
    @Override
    public Boolean startServuce(Long driverId) {
        //1.判断完成认证
        DriverLoginVo driverLoginVo = driverInfoFeignClient.getDriverLoginInfo(driverId).getData();
        if (driverLoginVo.getAuthStatus() != 2){
            throw new GuiguException(ResultCodeEnum.AUTH_ERROR);
        }

        //2.判断是否人脸
        Boolean isFace = driverInfoFeignClient.isFaceRecognition(driverId).getData();
        if (!isFace){
            throw new GuiguException(ResultCodeEnum.FACE_ERROR);
        }

        //3.更新订单状态 1：开始接单
        driverInfoFeignClient.updateServiceStatus(driverId,1);

        //4.删除redis司机位置信息
        locationFeignClient.removeDriverLocation(driverId);

        //5.清空司机临时队列数据
        newOrderFeignClient.clearNewOrderQueueData(driverId);

        return true;
    }

    /**
     * 停止接单服务
     * @param driverId
     * @return
     */
    @Override
    public Boolean stopService(Long driverId) {
        driverInfoFeignClient.updateServiceStatus(driverId,0);
        locationFeignClient.removeDriverLocation(driverId);
        newOrderFeignClient.clearNewOrderQueueData(driverId);
        return true;
    }

}
